package com.zttek.photosShareProject.modules.web.share;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.base.MyBaseController;
import com.zttek.photosShareProject.common.json.InfoVO;
import com.zttek.photosShareProject.common.json.NoticeVO;
import com.zttek.photosShareProject.modules.model.Info;
import com.zttek.photosShareProject.modules.model.Notice;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.service.share.IImageService;
import com.zttek.photosShareProject.modules.service.share.IInfoService;
import com.zttek.photosShareProject.modules.service.share.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Y
 * Date: 2017-09-01
 * Time: 10:31
 */
@RequestMapping("/info")
@Controller
public class InfoController extends MyBaseController {

    @Autowired
    private IInfoService infoService;

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IImageService imageService;

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public Object getList(@RequestBody InfoVO.GetListForm form) throws Exception {
        Page<Info> page = new Page<>(form.getPageNum(), form.getPageSize(), form.getOrderByField());
        page.setAsc(form.isAscend());
        infoService.selectInfo(page, form.getUserId(), form.getRange());
        List<Info> infos = page.getRecords();
        List<InfoVO> list = infoService.info2VO(infos);
        return renderSuccess(list);
    }

    @RequestMapping(value = "/noticeShare", method = RequestMethod.POST)
    @ResponseBody
    public Object noticeShare(@RequestParam("toUserId") Long toUserId,
                              @RequestParam("shareId") Long shareId,
                              @RequestParam("content") String content) throws Exception {
        infoService.createSystemInfoForShare(toUserId, shareId, content);
        return renderSuccess();
    }

    @RequestMapping(value = "/noticeComment", method = RequestMethod.POST)
    @ResponseBody
    public Object noticeComment(@RequestParam("toUserId") Long toUserId,
                                @RequestParam("commentId") Long commentId,
                                @RequestParam("content") String content) throws Exception {
        infoService.createSystemInfoForComment(toUserId, commentId, content);
        return renderSuccess();
    }

    @RequestMapping(value = "/noticeInfo", method = RequestMethod.POST)
    @ResponseBody
    public Object noticeInfo(@RequestParam("toUserId") Long toUserId,
                             @RequestParam("content") String content) throws Exception {
        infoService.createSystemInfoForNotice(toUserId, content);
        return renderSuccess();
    }

    @RequestMapping("/countNew")
    @ResponseBody
    public Object countNew(@RequestParam("userId") Long userId) throws Exception {
        Integer count = infoService.countNewInfo(userId);
        return renderSuccess(count);
    }

    @RequestMapping(value = "/read", method = RequestMethod.POST)
    @ResponseBody
    public Object read(@RequestParam("infoIds") List<Long> infoId) throws Exception {
        infoService.read(infoId);
        return renderSuccess();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam("infoId") Long infoId) throws Exception {
        infoService.deleteInfo(infoId);
        return renderSuccess();
    }

    @RequestMapping(value = "/getNoticeList", method = RequestMethod.POST)
    @ResponseBody
    public Object getNoticeList(@RequestBody NoticeVO.GetListForm form) throws Exception{
        Page<Notice> page = new Page<>(form.getPageNum(), form.getPageSize(), form.getOrderByField());
        page.setAsc(form.isAscend());
        noticeService.selectNoticePage(page, form.getUserId());
        List<Notice> notices = page.getRecords();
        List<NoticeVO> list = noticeService.notice2VO(notices, form.getUserId());
        return renderSuccess(list);
    }

    @RequestMapping(value = "/readNotice", method = RequestMethod.POST)
    @ResponseBody
    public Object readNotice(@RequestParam("noticeId") Integer noticeId,
                             @RequestParam("userId") Long userId) throws Exception {
        noticeService.readNotice(noticeId, userId);
        return renderSuccess();
    }

    @RequestMapping(value = "/deleteNotice", method = RequestMethod.POST)
    @ResponseBody
    public Object deleteNotice(@RequestParam("noticeId") Integer noticeId,
                               @RequestParam("userId") Long userId) throws Exception {
        noticeService.deleteNotice(noticeId, userId);
        return renderSuccess();
    }

}
